const { startProcess, qiniuUpload } = require('../libs/utils'),
       config = require('../config/config'),
       { CRAWLER } = require('../config/error_config'),
       {  returnInfo } = require('../libs/utils'), 
       {  addSliderData  } = require('../services/Slider'), 
       {  addAgencyInfo  }  = require('../services/AgencyInfo'),
       {  addRecomCourse } = require('../services/RecomCourse'),
       { addCollection } = require('../services/Collection'),
       { addTeacherData } = require('../services/Teacher'),
       { addStudentData } = require('../services/Student'),
       { addCourseTabData } = require('../services/CourseTab'),
       { addCourseData } = require('../services/Course'),
       { addAboutusData } = require('../services/Aboutus');
    

class Crawler {
   async crawlSliderDate(ctx, next) {
    const resData = new Promise((resolve, reject) => {
      startProcess({
         file: 'slider',
         async message(data) {
            data.map(async item => {
               if (item.imgUrl && !item.imgkey) {
                  const qiniu = config.qiniu;
                  try {
                     const imgData = await qiniuUpload({
                        url: item.imgUrl,
                        bucket: qiniu.bucket.tximg.bucket_name,
                        ext: '.jpg'
                     });
                     if (imgData.key) {
                        item.imgKey = imgData.key
                     }
                     const result = await addSliderData(item);
                     if (result) {
                        console.log('Data create ok');
                     } else {
                        console.log('Data create failed')
                     }
                  } catch (error) {
                     console.log(error);
                  }
               }
               console.log(data)
            });
            resolve(returnInfo(CRAWLER.CRAWL_SUCCESS))

         },
         async exit(code) {
            console.log(code)
         },
         async error(code) {
             resolve(returnInfo(CRAWLER.CRAWL_FAILED))
         },
      })
     })
     ctx.body = await resData;
   }
   //获取机构好评度等信息
   async crawlAgencyInfo(ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'agencyInfo',
            async message(data) {
               const qiniu = config.qiniu;
               if (data.logoUrl && !data.logokey) {
                  try {
                     const logoData = await qiniuUpload({
                        url: data.logoUrl,
                        bucket: qiniu.bucket.tximg.bucket_name,
                        ext: '.jpg'
                     })
                     if (logoData.key) {
                        data.logoKey = logoData.key;
                     }
                     const result = await addAgencyInfo(data);
                     if (result) {
                        console.log('Data create ok');
                     } else {
                        console.log('Data create failed');
                     }
                  } catch (err) {
                     console.log(err)
                  }
               }
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code);
            },
            async error(error) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            }
         })
      })
     ctx.body = await resData;
   }
   //推荐课程
   async crawlRecomCourse(ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'recomCourse',
            async message(data) {
               data.map(async item => {
                  console.log(item)
                  const qiniu = config.qiniu;
                  try {
                     if (item.posterUrl && !item.posterKey) {
                        const posterData = await qiniuUpload({
                           url: item.posterUrl,
                           bucket: qiniu.bucket.tximg.bucket_name,
                           ext: '.jpg'
                        });
                        if (posterData.key) {
                           item.posterKey = posterData.key
                        }
                     }
                     if (item.teacherImg && !item.teacherImgKey) {
                        const teacherImgData = await qiniuUpload({
                           url: item.teacherImg,
                           bucket: qiniu.bucket.tximg.bucket_name,
                           ext: '.jpg'
                        });
                        if (teacherImgData.key) {
                           item.teacherImgKey = teacherImgData.key;
                        }
   
                     }
                     const result = await addRecomCourse(item);
                     if (result) {
                        console.log('Data create ok');
                     } else {
                        console.log('Data create failed');
                     }
                  } catch (error) {
                     console.log(error)
                  }
   
                  console.log(data)
               })
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code)
            },
            async error(code) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            },
         })
      })
     ctx.body = await resData;
   }
   //课程集合
   async crawlCollection(ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'collection',
            async message(data) {
               data.map(async item => {
                  if (item.posterUrl && !item.posterkey) {
                     const qiniu = config.qiniu;
                     try {
                        const posterData = await qiniuUpload({
                           url: item.posterUrl,
                           bucket: qiniu.bucket.tximg.bucket_name,
                           ext: '.jpg'
                        });
                        if (posterData.key) {
                           item.posterkey = posterData.key
                        }
                        const result = await addCollection(item);
                        if (result) {
                           console.log('Data create ok');
                        } else {
                           console.log('Data create failed')
                        }
                     } catch (error) {
                        console.log(error);
                     }
                  }
                  console.log(data)
               })
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code)
            },
            async error(code) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            },
         })
      })
     ctx.body = await resData;
   }
   //明星老师集合
   async crawlTeacher(ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'teacher',
            async message(data) {
               data.map(async item => {
                  console.log(item)
                  if (item.teacherImg && !item.teacherImgKey) {
                     const qiniu = config.qiniu;
                     try {
                        const teacherData = await qiniuUpload({
                           url: item.teacherImg,
                           bucket: qiniu.bucket.tximg.bucket_name,
                           ext: '.jpg'
                        });
                        if (teacherData.key) {
                           item.teacherImgKey = teacherData.key
                        }
                        const result = await addTeacherData(item);
                        if (result) {
                           console.log('Data create ok');
                        } else {
                           console.log('Data create failed')
                        }
                     } catch (error) {
                        console.log(error);
                     }
                  }
                  console.log(data)
               })
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code)
            },
            async error(code) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            },
         })
      })
      ctx.body = await resData;
   }
   //优秀学生
   async crawlStudent(ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'student',
            async message(data) {
               data.map(async item => {
                  console.log(item)
                  if (item.studentImg && !item.studentImgKey) {
                     const qiniu = config.qiniu;
                     try {
                        const studentImg = await qiniuUpload({
                           url: item.studentImg,
                           bucket: qiniu.bucket.tximg.bucket_name,
                           ext: '.jpg'
                        });
                        if (studentImg.key) {
                           item.studentImgKey = studentImg.key
                        }
                        const result = await addStudentData(item);
                        if (result) {
                           console.log('Data create ok');
                        } else {
                           console.log('Data create failed')
                        }
                     } catch (error) {
                        console.log(error);
                     }
                  }
               })
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code)
            },
            async error(code) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            },
         })
      })
     ctx.body = resData;
   }
   //从课程选项卡
   async crawlSourceTab(ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'courseTab',
            async message(data) {
               data.map(async item => {
                 const result = await addCourseTabData(item);
                 if (result) {
                    console.log('Data create ok');
                 } else {
                    console.log('Data create failed')
                 }
               })
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code)
            },
            async error(code) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            },
         })
      })
     ctx.body = await resData;
   }
   //课程列表
   async crawlCourseData (ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'course',
            async message(data) {
               data.map(async item => {
                  console.log(item)
                  const qiniu = config.qiniu;
                  try {
                     if (item.posterUrl && !item.posterKey) {
                        const posterData = await qiniuUpload({
                           url: item.posterUrl,
                           bucket: qiniu.bucket.tximg.bucket_name,
                           ext: '.jpg'
                        });
                        if (posterData.key) {
                           item.posterKey = posterData.key
                        }
                     }
                     const result = await addCourseData(item);
                     if (result) {
                        console.log('Data create ok');
                     } else {
                        console.log('Data create failed');
                     }
                  } catch (error) {
                     console.log(error)
                  }
   
                  console.log(data)
               })
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code)
            },
            async error(code) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            },
         })
      })
      ctx.body = await resData;
   }
   //关于我们数据
   async crawlAboutsData(ctx, next) {
      const resData = await new Promise((resolve, reject) => {
         startProcess({
            file: 'aboutus',
            async message(data) {
               console.log(data)
                  const qiniu = config.qiniu;
                  if (data.posterUrl && !data.posterKey) {
                  try {
                        const posterData = await qiniuUpload({
                           url: data.posterUrl,
                           bucket: qiniu.bucket.tximg.bucket_name,
                           ext: '.jpg'
                        });
                        if (posterData.key) {
                           data.posterKey = posterData.key
                        }
                     
                     const result = await addAboutusData(data);
                     if (result) {
                        console.log('Data create ok');
                     } else {
                        console.log('Data create failed');
                     }
                  } catch (error) {
                     console.log(error)
                  }
               }
               resolve(returnInfo(CRAWLER.CRAWL_SUCCESS));
            },
            async exit(code) {
               console.log(code)
            },
            async error(code) {
               resolve(returnInfo(CRAWLER.CRAWL_FAILED));
            },
         })
      })
     ctx.body = await resData;
   }
}
module.exports = new Crawler()